package model;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 *
 * @author Sonny Sandberg
 */
public class CustomerSpecified 
{
    private CustomerName customerName;
    private Telephone customerTelephone;
    private Address customerAddress;
    private ArrayList<ActionListener> listeners;

    public CustomerSpecified(CustomerName customerName, Telephone customerTelephone, Address customerAddress)
    {
        this.customerName = customerName;
        this.customerTelephone = customerTelephone;
        this.customerAddress = customerAddress;
        listeners = new ArrayList();
        notifyListeners();
    }

    public CustomerSpecified(CustomerName customerName, Telephone customerTelephone)
    {
        this.customerName = customerName;
        this.customerTelephone = customerTelephone;
        customerAddress = null;
        listeners = new ArrayList();
        notifyListeners();
    }
    
    public CustomerSpecified()
    {
        customerName = new CustomerName();
        customerTelephone = new Telephone();
        customerAddress = new Address();
        listeners = new ArrayList();
        reset();
    }
    
    public void reset()
    {
        customerName.reset();
        customerTelephone.reset();
        customerTelephone.setCustomer(customerName);
        customerAddress.reset();
        notifyListeners();
    }

    public CustomerName getCustomerName()
    {
        return customerName;
    }

    public void setCustomerName(CustomerName customerName)
    {
        this.customerName = customerName;
        notifyListeners();
    }

    public Telephone getCustomerTelephone()
    {
        return customerTelephone;
    }

    public void setCustomerTelephone(Telephone customerTelephone)
    {
        this.customerTelephone = customerTelephone;
        notifyListeners();
    }

    public Address getCustomerAddress()
    {
        return customerAddress;
    }

    public void setCustomerAddress(Address customerAddress)
    {
        this.customerAddress = customerAddress;
        notifyListeners();
    }
    
    public String toString()
    {
        String text = "";
        
        text = String.format("%s\n%s\n%s", customerName.toString(), customerAddress.toString(), customerTelephone.toString());
        
        return text;
    }
    
    public void addListener(ActionListener listener) 
    {
        listeners.add(listener);
    }

    private void notifyListeners() 
    {
        ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Something changed");
        for (ActionListener listener : listeners) 
        {
            listener.actionPerformed(event);
        }
    }
}